//
//  DeblockingViewController.m
//  EPBPro
//
//  Created by wucha on 2018/8/3.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "DeblockingViewController.h"
#import "HttpTokenAction.h"
#import "TransHistory.h"
#import "DeblockingTableViewCell.h"

@interface DeblockingViewController (){
    DeBlock deBlock;
}
@property (nonatomic,strong) NSArray *listArray;
@end

@implementation DeblockingViewController

-(id)initWithDBlock:(DeBlock)block{
    if (self == [super init]) {
        deBlock = block;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"解锁中";
    [self reloadData];
    self.tableView.separatorStyle =0;
    
}

-(void)reloadData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [HttpTokenAction postTokenDeBlockingHistory:dic block:^(id result, NSError *error) {
        if (!error) {
            self.listArray = result;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    DeblockingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[DeblockingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    // Configure the cell...
    TransHistory *transHistory = self.listArray[indexPath.row];
    [cell setDeblockingCell:transHistory withBlock:^{
        [self cancelRequest:transHistory];
    }];
    return cell;
}

-(void)cancelRequest:(TransHistory*)transH{
    [BlockAlertView showAlertWithTitle:@"确定取消解锁?" message:@"" alertViewStyle:UIAlertViewStyleDefault block:^(id clickView, NSInteger index) {
        if (index == 1) {
            [HttpTokenAction postCancelDeblocking:transH.id block:^(id result, NSError *error) {
                if(!error){
                    [self reloadData];
                    if(deBlock){
                        deBlock();
                    }
                }
            }];
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
